package com.example.demo.pattern.observerpattern.station;


import com.example.demo.pattern.observerpattern.display.Observer;

import java.util.ArrayList;
import java.util.List;

public class WeatherStation implements Subject {

    private List<Observer> observerList;

    private String temperature;
    private String humidity;
    private String pressure;

    public WeatherStation() {
        this.observerList = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        if (!this.observerList.contains(observer)) {
            this.observerList.add(observer);
        }
    }

    @Override
    public void removeObserver(Observer observer) {
        if (this.observerList.contains(observer)) {
            observerList.remove(observer);
        }
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observerList) {
            observer.update(this.temperature, this.humidity, this.pressure);
        }
    }

    private void measurementsChanges() {
        notifyObservers();
    }

    //天气变化时，外部会调用此方法
    public void setMeasurements(String temperature, String humidity, String pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanges();
    }
}
